package com.csproj.dispatch.resource;

import com.codahale.metrics.annotation.Timed;
import com.csproj.dispatch.service.ProductInfoService;
import com.csproj.dispatch.service.dto.ProductInfoDTO;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.system.domain.UserInfo;
import com.csproj.system.service.SysLogsService;
import com.csproj.system.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * REST controller for managing ProductInfo.
 * 货物信息Controller
 */
@RestController
@RequestMapping("/api/center/pir")
public class ProductInfoResource {

    private final Logger log = LoggerFactory.getLogger(ProductInfoResource.class);

    private static final String ENTITY_NAME = "productInfo";

    private final ProductInfoService productInfoService;
    private final UserInfoService userInfoService;
    private final SysLogsService sysLogsService;

    public ProductInfoResource(ProductInfoService productInfoService,UserInfoService userInfoService,SysLogsService sysLogsService) {
        this.productInfoService = productInfoService;
        this.sysLogsService = sysLogsService;
        this.userInfoService = userInfoService;
    }

    /**
     * 保存货物信息
     * @param productInfoDTO 货物信息
     * @return
     */
    @PostMapping("/productInfo")
    @Timed
    public BaseDTO insertProductInfo(@RequestBody ProductInfoDTO productInfoDTO){
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_create_product_info_list","");
        return productInfoService.save(productInfoDTO);
    }

    /**
     * 通过集装箱id获取货物信息以及商品详细信息
     * @param containerId 集装箱id
     * @return
     */
    @GetMapping("/productInfo/{containerId}")
    @Timed
    public BaseDTO searchProductInfoByContainerId(@PathVariable String containerId){
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_view_product_info_list","");
        return productInfoService.searchProductInfoByContainerIdId(containerId);
    }

    /**
     * 修改货物信息
     * @param id 货物信息主键
     * @param productInfoDTO 货物信息
     * @return
     */
    @PutMapping("/productInfo/{id}")
    @Timed
    public BaseDTO updateProductInfoById(@RequestBody ProductInfoDTO productInfoDTO, @PathVariable String id){
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_update_product_info_list","");
        return productInfoService.updateProductInfo(id, productInfoDTO);
    }

    /**
     * 删除货物信息
     * @param id 货物信息主键
     * @return
     */
    @DeleteMapping("/productInfo/{id}")
    @Timed
    public BaseDTO deleteProductInfoById(@PathVariable String id){
        return productInfoService.deleteProductInfoById(id);
    }


}